//
//  EMReplyKeyBoard.m
//  PictureSelector
//
//  Created by Lyy on 15/8/24.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

#define KScreenHeight [UIScreen mainScreen].bounds.size.height
#define KScreenWidth [UIScreen mainScreen].bounds.size.width

#import "EMReplyKeyBoard.h"

@interface EMReplyKeyBoard ()<UITextViewDelegate>

@property (nonatomic,strong)UIViewController *viewController;
@property (nonatomic,strong)UIView *backView;
@property (nonatomic,strong)UIView *textViewBgView;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UIButton *sendButton;


@property (nonatomic) BOOL isTop;//用来判断评论按钮的位置

@end

@implementation EMReplyKeyBoard

static EMReplyKeyBoard *customKeyboard = nil;

+(EMReplyKeyBoard *)customKeyboard
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [[EMReplyKeyBoard alloc] init];
            [[NSNotificationCenter defaultCenter] addObserver:customKeyboard selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:customKeyboard selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:customKeyboard selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
        }
        return customKeyboard;
    }
}

+(id)allocWithZone:(struct _NSZone *)zone //确保使用者alloc时 返回的对象也是实例本身
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [super allocWithZone:zone];
        }
        return customKeyboard;
    }
}
+(id)copyWithZone:(struct _NSZone *)zone //确保使用者copy时 返回的对象也是实例本身
{
    return customKeyboard;
}

-(void)textViewShowView:(UIViewController *)viewController customKeyboardDelegate:(id)delegate
{
    self.viewController =viewController;
//    self.mDelegate =delegate;
    self.isTop = NO;//初始化的时候设为NO
    self.backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    NSLog(@"%p",self.backView);
    self.backView.backgroundColor =[UIColor colorWithWhite:0 alpha:0.1];
    [self.viewController.view addSubview:self.backView];
    
    self.textViewBgView =[[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 40)];
    self.textViewBgView.backgroundColor =[UIColor lightGrayColor];
    [self.backView addSubview:self.textViewBgView];
    
    self.textView = [[UITextView alloc] init];
    self.textView.layer.cornerRadius = 5;
    self.textView.frame = CGRectMake(5, 5, KScreenWidth - 60, 30);
    self.textView.backgroundColor =[UIColor whiteColor];
    self.textView.delegate = self;
    self.textView.text = @"我来说几句....";
    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.textView.font = [UIFont systemFontOfSize:17];
    [self.textViewBgView addSubview:self.textView];
    
    [self.textView becomeFirstResponder];
    
    self.sendButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [self.sendButton addTarget:self action:@selector(forTalk) forControlEvents:UIControlEventTouchUpInside];
    self.sendButton.frame =CGRectMake(CGRectGetMaxX(self.textView.frame),5,40,40);
    [self.sendButton setTintColor:[UIColor blackColor]];
    [self.textViewBgView addSubview:self.sendButton];
    
}

- (void)dismiss {
    [self.backView removeFromSuperview];
}

- (void)forTalk {
    
}

-(void)keyboardWillShow:(NSNotification *)note
{
    CGRect keyBoardRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardRect.size.height;
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        self.textViewBgView.transform = CGAffineTransformMakeTranslation(0, -keyBoardHeight- 40);
    }];
}

-(void)keyboardWillHide:(NSNotification *)note
{

    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        
        self.textViewBgView.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        [self dismiss];
    }];
    
}
- (void)textDidChanged:(NSNotification *)note {
    

    CGSize contentSize = self.textView.contentSize;
    if (contentSize.height > 80){
        return;
    }
    
    [self.textView setScrollsToTop:NO];
    
    CGFloat textViewBgViewRectHeight = contentSize.height;
    
    if (textViewBgViewRectHeight < 30) {
        textViewBgViewRectHeight = 30;
    }
    
//    self.textView
    
    CGRect textViewBgViewRect = self.textViewBgView.frame;
    textViewBgViewRect.origin.y -= textViewBgViewRectHeight+10 - textViewBgViewRect.size.height;
    textViewBgViewRect.size.height = textViewBgViewRectHeight+10;
    self.textViewBgView.frame = textViewBgViewRect;
    
    CGRect textViewRect = self.textView.frame;
    textViewRect.size.height = textViewBgViewRectHeight;
    self.textView.frame = textViewRect;

    CGRect sendButtonRect = self.sendButton.frame;
    sendButtonRect.origin.y = (textViewBgViewRect.size.height - sendButtonRect.size.height)/2.0;
    self.sendButton.frame = sendButtonRect;
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if (range.location>=140)
    {
        //控制输入文本的长度
        return  NO;
    }
    return YES;

}

- (void)textViewDidChangeSelection:(UITextView *)textView {

    [self.textView scrollRectToVisible:CGRectOffset(self.textView.frame, 0, -10) animated:YES];
    
}

-(void)dealloc //移除通知
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

@end
